﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using cms.libs;
using Newtonsoft.Json;
namespace CMS
{
    /// <summary>
    /// Summary description for ChartApi
    /// </summary>
    public class ChartApi : IHttpHandler
    {
        [Serializable]
        public class ReportDoanhthu
        {
            public List<string> label { get; set; }
            public List<string> sumBetMoney { get; set; }
            public List<string> sumWinMoney { get; set; }
            public List<string> sumBetNewMoney { get; set; }
            public List<string> sumWinNewMoney { get; set; }
            public List<string> sumProfitMoney { get; set; }
        }

        public void ProcessRequest(HttpContext context)
        {
            switch (context.Request["t"])
            {
                case "report":
                    GetReportDoanhThu(context);
                    break;
                case "report1":
                    GetReportDoanhThu1(context);
                    break;
                case "detailsBetLog":
                    ViewDetailsBetLog(context);
                    break;
            }
        }

        private void GetReportDoanhThu(HttpContext context)
        {
            ReportDoanhthu response = new ReportDoanhthu();
            try
            {
                DateTime fromD = DateTime.Now.AddMonths(-2);
                DateTime toD = DateTime.Now.AddDays(2);

                string strDateFrom = context.Request.QueryString["fd"];
                string strDateTo = context.Request.QueryString["td"];

                IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);
                DateTime.TryParse(strDateFrom, culture, System.Globalization.DateTimeStyles.AssumeLocal, out fromD);
                DateTime.TryParse(strDateTo, culture, System.Globalization.DateTimeStyles.AssumeLocal, out toD);


                DataTable lstDt = new DBIncome().GetListDataTable(fromD, toD);

                List<string> lstLabel = new List<string>();
                List<string> lstsumBetMoney = new List<string>();
                List<string> lstsumWinMoney = new List<string>();
                List<string> lstsumBetNewMoney = new List<string>();
                List<string> lstsumWinNewMoney = new List<string>();
                List<string> lstsumProfitMoney = new List<string>();

                for (int i = lstDt.Rows.Count - 1; i >= 0; i--)
                {
                    lstLabel.Add(lstDt.Rows[i][0].ToString());
                    lstsumBetMoney.Add(lstDt.Rows[i][4].ToString());
                    lstsumWinMoney.Add(lstDt.Rows[i][5].ToString());
                    lstsumBetNewMoney.Add(lstDt.Rows[i][8].ToString());
                    lstsumWinNewMoney.Add(lstDt.Rows[i][9].ToString());
                    lstsumProfitMoney.Add(lstDt.Rows[i][10].ToString());
                }
                response.label = lstLabel;
                response.sumBetMoney = lstsumBetMoney;
                response.sumWinMoney = lstsumWinMoney;
                response.sumBetNewMoney = lstsumBetNewMoney;
                response.sumWinNewMoney = lstsumWinNewMoney;
                response.sumProfitMoney = lstsumProfitMoney;

                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(response));
            }
            catch (Exception ex)
            {
                context.Response.ContentType = "application/json";
                context.Response.Write("[]");
            }
        }
        private void GetReportDoanhThu1(HttpContext context)
        {
            try
            {
                DateTime fromD = DateTime.Now.AddMonths(-2);
                DateTime toD = DateTime.Now.AddDays(2);

                string strDateFrom = context.Request.QueryString["fd"];
                string strDateTo = context.Request.QueryString["td"];

                IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);
                DateTime.TryParse(strDateFrom, culture, System.Globalization.DateTimeStyles.AssumeLocal, out fromD);
                DateTime.TryParse(strDateTo, culture, System.Globalization.DateTimeStyles.AssumeLocal, out toD);

                DataTable lstDt = new DBIncome().GetListDataTable(fromD, toD);


                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(lstDt));
            }
            catch (Exception ex)
            {
                context.Response.ContentType = "application/json";
                context.Response.Write("[]");
            }
        }
        private void ViewDetailsBetLog(HttpContext context)
        {
            BetLogDetailJson resp = new BetLogDetailJson();
            int betId = 0;
            int.TryParse(context.Request.QueryString["id"], out betId);
            string jsondetail = "";
            string result = "";
            int roomid = 0;
            string betline = "";
            string bonusGame = "";
            new DBAccountReport().GetInfoWithResult(betId, ref jsondetail, ref result, ref roomid, ref betline, ref bonusGame);
            resp.result = result;
            resp.jsondetail = jsondetail;
            resp.roomid = roomid;
            resp.betline = betline;
            resp.bonnus = bonusGame;
            context.Response.ContentType = "application/json";
            //context.Response.Write(jsondetail);
            context.Response.Write(JsonConvert.SerializeObject(resp));
        }

        [Serializable]
        public class BetLogDetailJson
        {
            public string result { get; set; }
            public string jsondetail { get; set; }
            public int roomid { get; set; }
            public string betline { get; set; }
            public string bonnus { get; set; }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}